﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QLTV_Data
{
 public class KetNoiCSDL_Data
    {
     SqlConnection conn = new SqlConnection("Data source=(local);Initial Catalog=ThuVienHocVien;User ID=sa;password=vanmanh");
     public KetNoiCSDL_Data()
     {
         if (conn.State == ConnectionState.Closed)
             conn.Open();
     }

     public DataTable loadAnh(string sql)
     {
         SqlCommand cmd = new SqlCommand(sql, conn);
         DataTable dt = new DataTable();
         SqlDataReader ds = cmd.ExecuteReader();
         dt.Load(ds);
         return (dt);
     }
     public DataTable taobang(string sql)
     {
         DataTable dt = new DataTable();
         SqlDataAdapter ds = new SqlDataAdapter(sql, conn);
         ds.Fill(dt);
         return (dt);
     }

     public DataTable LayDuLieu(string sql)
     {
         SqlCommand cmd = new SqlCommand(sql,conn);
         cmd.CommandType = CommandType.StoredProcedure;
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataTable dt = new DataTable();
         da.Fill(dt);
         return dt;
     }
     public DataTable LayDuLieu(string sql,string[]name,object[]value,int Nparameter)
     {
         SqlCommand cmd = new SqlCommand(sql,conn);
         cmd.CommandType = CommandType.StoredProcedure;
         for (int i = 0; i < Nparameter; i++)
         {
             cmd.Parameters.AddWithValue(name[i],value[i]);
         }
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataTable dt = new DataTable();
         da.Fill(dt);
         return dt;
     }
     public int update(string sql)
     {
         SqlCommand cmd = new SqlCommand(sql, conn);
         cmd.CommandType = CommandType.StoredProcedure;
         return cmd.ExecuteNonQuery();
     }
     public  int update(string sql,string[]name,object[]value,int Nparameter)
     {
         SqlCommand cmd=new SqlCommand(sql,conn);
         cmd.CommandType=CommandType.StoredProcedure;
         for (int i = 0; i < Nparameter; i++)
         {
             cmd.Parameters.AddWithValue(name[i],value[i]);
         }
         return cmd.ExecuteNonQuery();
     }
    }

}
